package com.nineclock.document.controller;

import com.nineclock.common.entity.Result;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFolderDto;
import com.nineclock.document.dto.DocumentDto;
import com.nineclock.document.service.DocumentService;
import com.nineclock.document.AppDocumentControllerApi;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@RestController
public class DocumentController implements AppDocumentControllerApi {

    @Autowired
    DocumentService documentService;

    /**
     * 根据父目录id,查询文件夹及文件信息
     * @param parentFolderId
     * @return
     */
    @Override
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> queryFolderAndFile(@RequestParam(value = "parentFoldId",required = false,defaultValue = "0") Long parentFolderId) {
        DocumentDto documentDTO = documentService.queryFolderAndFile(parentFolderId);

        return Result.success(documentDTO);
    }

    /**
     * 新建文件夹
     * @param docFolderDto
     * @return
     */
    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto docFolderDto) {
        documentService.insertFolder(docFolderDto);
        return Result.success();
    }

    /**
     * 新建文档
     * @param docFileDto
     * @return
     */
    @Override
    @PostMapping("/insertFile")
    public Result insertFile(@RequestBody DocFileDto docFileDto) {
        documentService.insertFile(docFileDto);
        return Result.success();
    }

    /**
     * 根据id查询文档
     * @param fileId
     * @return
     */
    @Override
    @GetMapping("/getFileByFileId")
    public Result<DocFileDto> getFileByFileId(@RequestParam("id") Long fileId) {
        DocFileDto docFileDto = documentService.getFileByFileId(fileId);
        return Result.success(docFileDto);
    }

    /**
     * 根据ID更新文档内容
     * @param docFileDto
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping("/updateFile")
    @Override
    public Result updateFile(DocFileDto docFileDto) throws UnsupportedEncodingException {

        documentService.updateFile(docFileDto);
        return Result.success();

    }

    @Override
    @GetMapping("/pagingFile")
    public Result<DocumentDto> pagingFile(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize,@RequestParam("keyword") String keyword) {
        DocumentDto documentDto = documentService.pagingFile(page,pageSize, keyword);
        return Result.success(documentDto);

    }
}
